﻿using Microsoft.AspNetCore.Http;
using NPOI.HSSF.UserModel;
using Publicuse.Entity;
using Publicuse.Interface;
using Publicuse.Util;
using Publicuse.Util.Excel;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publicuse.Cache
{
    public class ImportCache
    {
        private static ConcurrentDictionary<string, string> cidlist = new ConcurrentDictionary<string, string>();
        private static ConcurrentDictionary<string, string> cidlistb = new ConcurrentDictionary<string, string>();
        private static ConcurrentDictionary<string, User> userlist = new ConcurrentDictionary<string, User>();

        public static void InitImportData(IFormFileCollection files, string path, string importfile, out string msg,
            Func<List<DataTable>, bool> action, bool isAnnotation = true)
        {
            msg = string.Empty;
            string filePath = string.Empty;
            List<DataTable> dtlist = new List<DataTable>();
            if (files != null)
            {
                foreach (var formFile in files)
                {
                    string fileName = Path.GetExtension(formFile.FileName);
                    if (fileName != ".xls")
                    {
                        msg = "请选择扩展名为.xls的excel格式文件~~";
                        break;
                    }
                    if (formFile.Length > 0)
                    {
                        HSSFWorkbook hSSFWorkbook = new HSSFWorkbook(formFile.OpenReadStream());
                        dtlist = ExcelOperationUtil.ToExcelDateTable(hSSFWorkbook);
                        action.Invoke(dtlist);
                        //var module = dtlist[0].Rows[0].ItemArray[0].ToString();
                        //if (module.Contains(importfile))
                        //{
                        //    action.Invoke(dtlist);
                        //}
                        //else
                        //{
                        //    if (dtlist[0].Rows.Count == 0)
                        //    {
                        //        msg = "上传文件不是预期的excle数据文件";
                        //    }
                        //}
                    }
                    //if (formFile.Length > 0)
                    //{
                    //    long fileSize = formFile.Length;
                    //    path = path + "\\Files\\Uploads\\" + DateTime.Now.ToString("yyyyMMdd") + "\\";
                    //    path = path.Replace("\\", "/"); // Linux系统部署路径需要改成这样
                    //    if (!FileUtil.ExistsDir(path))
                    //        FileUtil.CreateDirectory(path);

                    //    filePath = path + formFile.FileName;
                    //    using (var stream = new FileStream(filePath, FileMode.Create))
                    //    {
                    //        formFile.CopyTo(stream);
                    //    }
                    //    //dtlist = ExcelUtil.GetExcelUtil.LoadTableFromExcel(filePath);
                    //    var data = OfficeUtil.ReadExcelToDataTable(filePath, out var sheetname, isAnnotation, null, true);
                    //    dtlist.Add(data);
                    //    var module = dtlist[0].Rows[0].ItemArray[0].ToString();
                    //    if (module.Contains(importfile))
                    //    {
                    //        action.Invoke(dtlist);
                    //    }
                    //    else
                    //    {
                    //        if (dtlist[0].Rows.Count == 0)
                    //        {
                    //            msg = "上传文件不是预期的excle数据文件";
                    //        }
                    //    }
                    //    action.Invoke(dtlist);
                    //}
                }
            }
            else
            {
                var data = OfficeUtil.ReadExcelToDataTable(path, out var sheetname, isAnnotation, null, true);
                dtlist.Add(data);
                action.Invoke(dtlist);
            }
        }

    }
}
